
local tostring = tostring
local string = string
local io = io
local io_type = io.type
local os = os
local table = table
local pairs = pairs
local type = type

local _M = {
    _version = "1.0"
}
local mt = { __index = _M }

function _M.new(self,log_level,log_path)    
    local log_file,err = io.open(log_path,"a")
    if not log_file then
        return nil,err
    end
    
    return setmetatable({ log_level = log_level,log_file = log_file,log_path=log_path}, mt)    
end

function _M.gcs_log(self,info,text)
    local loglevel = self.log_level          --log级别配置,debug,error,info,not
    local log_file = self.log_file
    local infonum = 0
    local loglevelnum = 0
    if info == "debug" then
        infonum = 5
    elseif info == "info" then
        infonum = 3
    elseif info == "error" then
        infonum = 1
    end
    if loglevel == "debug"  then
        loglevelnum = 5
    elseif loglevel == "info" then
        loglevelnum = 3
    elseif loglevel == "error" then
        loglevelnum = 1
    end
    
    if infonum <= loglevelnum then
        local isfile = io_type(log_file)
        if isfile == "closed file" then
            self.log_file = io.open(self.log_path,"a")
        end
        local timenum = os.time()
        local timestr = tostring(timenum)
        timestr = string.sub(timestr,11)--取ms
        timestr = "["..os.date("%Y-%m-%d %H:%M:%S",timenum)..timestr.."]"
        log_file:write(timestr..":["..info.."]:"..text)
        log_file:write("\r\n")
        log_file:flush()
    end
end

return _M

